﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using WLFramework.Validation;
using WLFramework.Json;
using WLFramework.Web;
using WLFramework.Data.Linq;
using GenericHtml;

namespace SampleSite.admin.ajax
{
    /// <summary>
    /// enableUserPosition 的摘要说明
    /// </summary>
    public class enableUserPosition : AjaxHttpHandler
    {
        private class InputData
        {
            [Required]
            public int U_ID { get; set; }

            [Required]
            public int P_ID { get; set; }

            [Required]
            public int P_State { get; set; }
        }


        public override void OnProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";

            InputData input;
            var errors = context.GetModel<InputData>(out input);
            if (errors.Count > 0)
                throw new Exception("参数错误");

            using (PermissionContext db = new PermissionContext())
            {
                db.BeginTransaction();
                db.sys_UserPosition.Where(k => k.U_ID == input.U_ID &&
                                               k.P_ID == input.P_ID)
                                   .Set(u => u.P_State, input.P_State == 0 ? 1 : 0)
                                   .Update();

                db.CommitTransaction();

                context.Response.Write(JSON.Instance.ToJSON(JSONResult.Success));
            }
        }
    }
}